﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GPSMap.Core.Misc.DAL;
using MapControl.Misc;
using MapControl.Misc.DAL;

namespace GPSMap
{
    public partial class frmPlaneList : Form
    {
        private PlaneRepository _objPlaneRespository;
        public bool isChanged = false; //Cho biết tình trạng có thay đổi thông tin không
        private frmGPSTracking frmParent;
        public frmPlaneList(frmGPSTracking frm)
        {
            InitializeComponent();
            frmParent = frm;
            HTLibrary.DataGridViewHelper.DataGridView_InitEvent(dataGridViewPlane);
            _objPlaneRespository = new PlaneRepository();
        }

        private void frmPlane_Load(object sender, EventArgs e)
        {
            BindingDataGridView();
            FormatDataGridView();
        }

        /// <summary>
        /// Refresh lại datagridview sau khi thêm mới hoặc cập nhật
        /// </summary>
        /// <param name="isInsert"></param>
        public void RefreshDataGridView(bool isInsert)
        {
            if (isInsert)
            {
                BindingDataGridView(dataGridViewPlane.Rows.Count);
            }
            else
            {
                int indexRow = dataGridViewPlane.CurrentCell != null ? dataGridViewPlane.CurrentCell.RowIndex : 0;
                BindingDataGridView(indexRow);
            }
        }

        public void BindingDataGridView(int indexRow = -1)
        {
            DataTable db = _objPlaneRespository.GetAll();
            BindingSource bsPlane = new BindingSource();
            bsPlane.DataSource = db.DefaultView;
            dataGridViewPlane.DataSource = bsPlane;
            bindingNavigator1.BindingSource = bsPlane;
            
            if (indexRow > 0)
            {
                if (indexRow == dataGridViewPlane.Rows.Count)
                    indexRow--;
                foreach (DataGridViewColumn col in dataGridViewPlane.Columns)
                {
                    if (col.Visible)
                    {
                        dataGridViewPlane.CurrentCell = dataGridViewPlane.Rows[indexRow].Cells[col.Index];
                        break;
                    }
                }
            }
            else
            {
                if (dataGridViewPlane.Rows.Count > 0)
                {
                    dataGridViewPlane.Rows[0].Selected = true;
                }
            }

        }

        private void FormatDataGridView()
        {
            dataGridViewPlane.ReadOnly = true;
            dataGridViewPlane.Columns["id"].Visible = false;
            dataGridViewPlane.Columns["code"].HeaderText = "Mã máy bay";
            dataGridViewPlane.Columns["name"].HeaderText = "Tên máy bay";
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int indexRow = dataGridViewPlane.CurrentCell != null ? dataGridViewPlane.CurrentCell.RowIndex : 0;
                string msg = string.Format("Bạn thật sự muốn xóa máy bay \"{0}\"?",
                                           dataGridViewPlane.Rows[indexRow].Cells["name"].Value);
                if (MessageBox.Show(msg, "Thông báo" , MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    _objPlaneRespository.Delete((int)dataGridViewPlane.Rows[indexRow].Cells["id"].Value);
                    indexRow--;
                    BindingDataGridView(indexRow);
                    //MessageBox.Show("Xóa thành công!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                frmPlane frm = new frmPlane(this);
                frm.Show();
            }
            catch (Exception ex) 
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                int indexRow = dataGridViewPlane.CurrentCell != null ? dataGridViewPlane.CurrentCell.RowIndex : 0;
                Plane obj = new Plane();
                obj.Id = (int)dataGridViewPlane.Rows[indexRow].Cells["id"].Value;
                obj.Code = dataGridViewPlane.Rows[indexRow].Cells["code"].Value.ToString();
                obj.Name = dataGridViewPlane.Rows[indexRow].Cells["name"].Value.ToString();
                frmPlane frm = new frmPlane(this, obj);
                frm.Show();
                //BindingDataGridView(indexRow);
                //dataGridViewPlane.Rows[indexRow].Selected = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void dataGridViewPlane_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void frmPlaneList_FormClosing(object sender, FormClosingEventArgs e)
        {
            frmParent.RefreshPlaneList(isChanged);
        }
    }
}